//
//  FileManager.m
//  OralRobot
//
//  Created by 王桂敏 on 16/6/29.
//  Copyright © 2016年 wangguimin. All rights reserved.
//

#import "FileManager.h"

@implementation FileManager

+ (float)fileSizeAtPath:(NSString *)path{
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if([fileManager fileExistsAtPath:path]){
        long long size = [fileManager attributesOfItemAtPath:path error:nil].fileSize;
        return size/1024.0/1024.0;
    }
    return 0;
}

+ (float)folderSizeAtPath:(NSString *)path{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    float folderSize;
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles = [fileManager subpathsAtPath:path];
        
        for (NSString *fileName in childerFiles) {
             
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            folderSize += [self fileSizeAtPath:absolutePath];
        }
        //SDWebImage框架自身计算缓存的实现
        folderSize += [[SDImageCache sharedImageCache] getSize]/1024.0/1024.0;
        
        return folderSize;
    }
    return 0;
}
+ (float)cacheSize{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray  *libPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *libPath = [libPaths lastObject];
    NSString *path = [libPath stringByAppendingPathComponent:@"Caches"];
    float folderSize;
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles = [fileManager subpathsAtPath:path];
        
        for (NSString *fileName in childerFiles) {
            if (![fileName isEqualToString:@"sound"]) {
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            folderSize += [self fileSizeAtPath:absolutePath];
            }
        }
        //SDWebImage框架自身计算缓存的实现
        folderSize += [[SDImageCache sharedImageCache] getSize]/1024.0/1024.0;
        
        return folderSize;
    }
    return 0;
}
+ (void)clearCache{
//    [self showIndeterminateWithHint:@"Cleaning up"];
    NSFileManager *fileManager=[NSFileManager defaultManager];
//    NSArray  *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//    NSString *docPath = [docPaths lastObject];
//    if ([fileManager fileExistsAtPath:docPath]) {
//        NSArray *childerFiles=[fileManager subpathsAtPath:docPath];
//        for (NSString *fileName in childerFiles) {
//            //如有需要，加入条件，过滤掉不想删除的文件
//            NSString *absolutePath=[docPath stringByAppendingPathComponent:fileName];
//            [fileManager removeItemAtPath:absolutePath error:nil];
//        }
//    }
//    
    NSArray  *libPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *libPath = [libPaths lastObject];
    
    if ([fileManager fileExistsAtPath:libPath]) {
        NSArray *childerFiles = [fileManager subpathsAtPath:libPath];
        for (NSString *fileName in childerFiles) {
            if (![fileName isEqualToString:@"sound"] && [fileName isEqualToString:@"Preferences"]) {
                //如有需要，加入条件，过滤掉不想删除的文件
                NSString *absolutePath=[libPath stringByAppendingPathComponent:fileName];
                [fileManager removeItemAtPath:absolutePath error:nil];
            }
            
        }
    }
    
//    NSArray  *cachePaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
//    NSString *cachePath = [cachePaths lastObject];
//    if ([fileManager fileExistsAtPath:cachePath]) {
//        NSArray *childerFiles = [fileManager subpathsAtPath:cachePath];
//        for (NSString *fileName in childerFiles) {
//            //如有需要，加入条件，过滤掉不想删除的文件
//            NSString *absolutePath=[cachePath stringByAppendingPathComponent:fileName];
//            [fileManager removeItemAtPath:absolutePath error:nil];
//        }
//    }
    [[SDImageCache sharedImageCache] cleanDisk];
//    [self hideHud];
}

@end
